package 力扣_滑动窗口.第一类模板;

/**
 * @author zx
 * @create 2022-05-15 20:42
 *
 * 类似于424
 */
public class 考试的最大困扰度_2024 {
    public int maxConsecutiveAnswers(String answerKey, int k) {
        return Math.max(maxConsecutiveChar(answerKey,k,'T'),
                maxConsecutiveChar(answerKey,k,'F'));
    }
    /**
     在只含有A,和B的字符串中,找出一个最长的子数组,该子数组内最多允许有k 个 B
     */
    private int maxConsecutiveChar(String str, int k, char B){
        int left = 0;
        int res = 0;
        int chars = 0;
        for(int right = 0;right < str.length();right++){
            if(str.charAt(right) == B){
                chars++;
            }
            while(chars > k){
                if(str.charAt(left) == B){
                    chars--;
                }
                left++;
            }
            res = Math.max(res,right - left + 1);
        }
        return res;
    }
}
